﻿using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace Kele.Activity
{
    public class FileService
    {
        private void test(int merchant_id, IFormFile file)
        {
            //var endpoint = "<yourEndpoint>";
            //var accessKeyId = "<yourAccessKeyId>";
            //var accessKeySecret = "<yourAccessKeySecret>";
            //var bucketName = "<yourBucketName>";
            //var objectName = "<yourObjectName>";
            //// 创建OssClient实例。
            //var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
            //try
            //{
            //    // 上传文件。
            //    client.PutObject(bucketName, objectName, file.OpenReadStream());
            //    Console.WriteLine("Put object succeeded");
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("Put object failed, {0}", ex.Message);
            //}
        }

        //Qiniu upload
        public static async Task<dynamic> CloudFileUpload(int merchant_id, IFormFile file)
        {
            //var mac = new Mac(ApiConfig.KeleActivitySetting.QiniuAccessKey, ApiConfig.KeleActivitySetting.QiniuSecretKey);
            //// 存储空间名
            //var Bucket = ApiConfig.KeleActivitySetting.QiniuBucket;
            //// 设置上传策略，详见：https://developer.qiniu.com/kodo/manual/1206/put-policy
            //var putPolicy = new PutPolicy { Scope = Bucket };
            //putPolicy.SetExpires(3600 * 24);
            //var token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());

            //var config = new Config
            //{
            //    Zone = Zone.ZONE_CN_East,
            //    UseHttps = true,
            //    UseCdnDomains = true,
            //    ChunkSize = ChunkUnit.U512K
            //};

            //// 上传文件名
            //var uid = ApiUtility.GenerateId();
            //var key = $"ump/merchant/{merchant_id}/{uid}";
            //var target = new FormUploader(config);
            //var url = ApiConfig.KeleActivitySetting.QiniuBaseUrl + key;
            //var result = await Task.Run(() => target.UploadStream(file.OpenReadStream(), key, token, null));

            //if (result.Code != 200) return null;
            //var json = JObject.Parse(result.Text);
            ////  var xKey = json.Value<string>("key");
            ////  var hash = json.Value<string>("hash");
            return new
            {
                //url,
                //name = file.FileName,
                //uid
            };
        }

        public async Task QiniuDelete(int merchant_id, string uid)
        {
            //var mac = new Mac(ApiConfig.KeleActivitySetting.QiniuAccessKey, ApiConfig.KeleActivitySetting.QiniuSecretKey);
            //var Bucket = ApiConfig.KeleActivitySetting.QiniuBucket;

            //var config = new Config { Zone = Zone.ZONE_CN_East };
            //var bucketManager = new BucketManager(mac, config);
            //var key = $"ump/merchant/{merchant_id}/{uid}";
            //var deleteRet = await Task.Run(() => bucketManager.Delete(Bucket, key));
            //if (deleteRet.Code != 200) throw new Exception(deleteRet.RefText);
        }
    }
}